//
//  YHYRecommendViewController.m
//  TripTogether
//
//  Created by 杨泓晔 on 15/12/26.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "YHYRecommendViewController.h"
#import "WJQHeadScrollCollectionViewCell.h"
#import "YHYRecommendHeadCollectionViewCell.h"

#import "YHYRecommendCenterCollectionViewCell.h"

#import "YHYReSubDetailViewController.h"


#import "WJQStyleDarkCollectionViewCell.h"
#import "WJQStyleLightCollectionViewCell.h"

#import "WJQEverydayModel.h"
#import "WJQCycleModel.h"

#import "WJQStyleDarkDetailViewController.h"

#import "YHYReSubEveryButtonViewController.h"

@interface YHYRecommendViewController ()<UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,YHYRecommendHeadDelegate>

@property (nonatomic,retain)UICollectionViewFlowLayout *flowlayout;


@property (nonatomic,retain) UICollectionView *collView;/**<*/

@property (nonatomic, strong) NSMutableDictionary *recommendDic;

@property (nonatomic, strong) NSMutableArray *cycleArray;

@property (nonatomic, strong) UIView *backgroundView; // 数据没加载出来之前view背景
@end

@implementation YHYRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = nil;
    self.view.backgroundColor = [UIColor colorWithRed:0.980 green:0.961 blue:0.910 alpha:1.000];
    _recommendDic = [NSMutableDictionary dictionary];
    _cycleArray = [NSMutableArray array];
self.navigationItem.title = @"带你转!";
    [self createCollectionView];
    [self createBackgroundView];
    [self getURL];
    
}
- (void)createBackgroundView
{
    _backgroundView = [UIView new];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_backgroundView];
    
    [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
    }];
}
- (void)getURL{
    
    /**
     *  key : 
        1 每日精选故事
        2 精彩游记和专题
     */
    
    
    [self MBShow];
    
    NSString *str =  @"http://api.breadtrip.com/v2/index/";
    
    [WJQNetwork getAFNet:str block:^(id result) {
        
        [_cycleArray removeAllObjects];
        [_recommendDic removeAllObjects];
        
        
        NSMutableDictionary *dic = result;
        NSMutableDictionary *dicElements = [dic objectForKey:@"data"];
        NSMutableArray *arrayElements = [dicElements objectForKey:@"elements"];


        NSMutableArray *everyArr = [NSMutableArray array];
        NSMutableArray *tripArr = [NSMutableArray array];
        NSMutableArray *cycleArr = [NSMutableArray array];
        
        for (NSMutableDictionary *dicElements in arrayElements) {
            
            NSMutableArray *dataArr = [dicElements objectForKey:@"data"];
            
            
            NSString *type = [NSString stringWithFormat:@"%@", [dicElements objectForKey:@"type"]];
            
            
            // 轮播图
            if ([type isEqualToString:@"1"]) {
                
                for (NSMutableDictionary *dic in [dataArr lastObject]) {
                    
                    WJQCycleModel *cycleMod = [WJQCycleModel modelWithDicationary:dic];
                    [cycleArr addObject:cycleMod];
                }
                
            }
            
            
            // 每日精选模块
            if ([type isEqualToString:@"10"]) {
                
                WJQEverydayModel *everyDayMod = [WJQEverydayModel modelWithDicationary:[dataArr lastObject]];
                
                [everyDayMod setValuesForKeysWithDictionary:[[dataArr lastObject] objectForKey:@"user"]];
                
                [everyArr addObject:everyDayMod];
            }
            
            // 精彩游记和专题
            
            // 推荐旅行产品
            if ([type isEqualToString:@"7"]) {
                
                WJQTripModel *tripMod = [WJQTripModel modelWithDicationary:[dataArr lastObject]];
                tripMod.type = type;
                [tripArr addObject:tripMod];
            }
    
            if ([type isEqualToString:@"4"]) {
                WJQTripModel *travelMod = [WJQTripModel modelWithDicationary:[dataArr lastObject]];
                
                travelMod.userName = [[[dataArr lastObject] objectForKey:@"user"] objectForKey:@"name"];
                travelMod.avatar_m = [[[dataArr lastObject] objectForKey:@"user"] objectForKey:@"avatar_m"];
                travelMod.type = type;
                
                [tripArr addObject:travelMod];
            }
        }
        
        // 将轮播图的链接取出来放进数组
        for (WJQCycleModel *mod in cycleArr) {
            [_cycleArray addObject:mod.image_url];
        }
        
        
        [_recommendDic setObject:cycleArr forKey:@"0"];
        [_recommendDic setObject:everyArr forKey:@"1"];
        [_recommendDic setObject:tripArr forKey:@"2"];
        
        
        [_collView reloadData];
        
        [_backgroundView removeFromSuperview];
        [self MBHide];
    } errorBlock:^(NSError *error) {
        
        [self NetworkError:^{
            
            
        }];
        
        
        
    }];
}


- (void)createCollectionView{
    
    _flowlayout = [UICollectionViewFlowLayout new];
    
    //..默认最小为10，如果比10小，需要重新付值
    _flowlayout.minimumInteritemSpacing = 5;
    //行
    _flowlayout.minimumLineSpacing = 10;


    _collView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:_flowlayout];
    _collView.backgroundColor  = [UIColor colorWithRed:0.980 green:0.961 blue:0.910 alpha:1.000];
    _collView.delegate = self;
    _collView.dataSource = self;
    [self.view addSubview:_collView];

    
    
    [_collView registerClass:[WJQHeadScrollCollectionViewCell class] forCellWithReuseIdentifier:@"HeadCell"];
    
    [_collView registerClass:[YHYRecommendCenterCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [_collView registerClass:[YHYRecommendHeadCollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView"];
    
    
    [_collView registerClass:[WJQStyleDarkCollectionViewCell class] forCellWithReuseIdentifier:@"StyleDark"];
    
    [_collView registerClass:[WJQStyleLightCollectionViewCell class] forCellWithReuseIdentifier:@"StyleLight"];
    
    
    [_collView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
        
    }];
    
    
}




#pragma mark HeadCell 增补视图

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 1 || indexPath.section == 2) {
        
        if (kind == UICollectionElementKindSectionHeader) {
            
            YHYRecommendHeadCollectionViewCell *cellHead = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeadView" forIndexPath:indexPath];
#pragma mark - 协议
            cellHead.myDelegate = self;
            
            if (1 == indexPath.section) {
                [cellHead setValue:@"每日精选故事" isHidden:NO];
            } else {
                [cellHead setValue:@"精彩游记和专题" isHidden:YES];
            }
            
            return cellHead;
        }
        
    }
    
    return nil;
}
- (void)presentButtonDeleagte{
    YHYReSubEveryButtonViewController *buttonVC = [YHYReSubEveryButtonViewController new];
    [self.navigationController pushViewController:buttonVC animated:YES];
    
}
// 头视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section > 0) {
        return CGSizeMake(SCREEN_WIDTH, 60);
    }
    return CGSizeMake(0, 0);
}
// 返回每个区的边界距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (1 == section) {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
    if (2 == section) {
        return UIEdgeInsetsMake(10, 0, 10, 0);
    }

    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
#pragma mark - collectionView 方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        NSMutableArray *array = _recommendDic[@"1"];
       // NSLog(@"%ld", array.count);
        return array.count;
    }
    else{
        NSMutableArray *array = _recommendDic[@"2"];
       // NSLog(@"%ld", array.count);
        return array.count;
    }
    
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        //滚动视图
        static NSString *cellHead = @"HeadCell";
        WJQHeadScrollCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellHead forIndexPath:indexPath];
        cell.array = _cycleArray;
        return cell;
    }
    //图片加文字＋ 四个Item
    else if (indexPath.section == 1) {
     
        static NSString *cellll = @"cell";
        YHYRecommendCenterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellll forIndexPath:indexPath];
        
        NSMutableArray *arr = _recommendDic[@"1"];
        cell.myModel = arr[indexPath.row];
        
        return  cell;
        
    }
    
    else {
        static NSString *darkStr = @"StyleDark";
        static NSString *lightStr = @"StyleLight";
        
        NSMutableArray *arr = _recommendDic[@"2"];
        WJQTripModel *model = arr[indexPath.row];

        
        if ([model.type isEqualToString:@"7"]) {
            WJQStyleDarkCollectionViewCell *darkCell = [collectionView dequeueReusableCellWithReuseIdentifier:darkStr forIndexPath:indexPath];
            
            darkCell.tripMod = model;
            return darkCell;
        } else {
            
            WJQStyleLightCollectionViewCell *lightCell = [collectionView dequeueReusableCellWithReuseIdentifier:lightStr forIndexPath:indexPath];
            
            lightCell.tripMod = model;
            return lightCell;
        }
    }
}
//did
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
       
    
    YHYReSubDetailViewController *yhyVC = [YHYReSubDetailViewController new];
        NSMutableArray *arr = _recommendDic[@"1"];
        WJQEverydayModel *model =arr[indexPath.row];

        yhyVC.everydayModel = model;
       
        yhyVC.navigationItem.title = @"故事详情";
        
    [self.navigationController pushViewController:yhyVC animated:yhyVC];
        
    } else if (2 == indexPath.section) {
        
        NSMutableArray *arr = _recommendDic[@"2"];
        WJQTripModel *model    = arr[indexPath.row];
        
        
        // 跳到dark页面
        if ([model.type isEqualToString:@"7"]) {
            WJQStyleDarkDetailViewController *darkVC = [[WJQStyleDarkDetailViewController alloc] init];
          //  darkVC.navigationItem.title = @"21";
            darkVC.url = model.url;
            [self.navigationController pushViewController:darkVC animated:YES];
            
        }

        
    }
    
}


#pragma mark - 重新定义layout 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        //重新定义大小
        return CGSizeMake(SCREEN_WIDTH, 200);
        
    } else
    if (indexPath.section == 1) {
#warning mark - _flowlayout....head

        return CGSizeMake((SCREEN_WIDTH - 30)/2, 230);
    }
    else{
#warning mark - _flowlayout....head

        return CGSizeMake((SCREEN_WIDTH - 16), 200);
    }
    
//    return CGSizeMake(300, 300);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
